﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace DALL.Procurement
{
    public class T_PUB_PURCHASE
    {
        public T_PUB_PURCHASE()
        { }

        /// <summary>
        /// 获取采购主题智能提示
        /// </summary>
        /// <param name="branch_id"></param>
        /// <returns></returns>
        public DataSet GetPurchaseApplyTitel(int branch_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITLE FROM T_PUB_PURCHASE ");
            strSql.Append(" where BRANCH_ID=" + branch_id);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        public DataSet GetPurchaseApplyTitel()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DISTINCT TITLE FROM T_PUB_PURCHASE ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        #region  Method
        public DataSet GetApplyList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TITLE,APPLICANT,APPLICANT_NAME,STATE,END_TIME,REAL_TIME,a.REMARK,APPLICANT_TIME,APPROVER,APPROVER_NAME,ORDER_ID,a.BRANCH_ID,NOTATION,BRANCH_NAME");
            strSql.Append(" FROM T_PUB_PURCHASE as a, T_PUB_BRANCH as b ");
            strSql.Append("where b.BRANCH_ID = a.BRANCH_ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Procurement.T_PUB_PURCHASE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_PUB_PURCHASE(");
            strSql.Append("ID,TITLE,APPLICANT,APPLICANT_NAME,STATE,END_TIME,REAL_TIME,REMARK,APPLICANT_TIME,APPROVER,APPROVER_NAME,ORDER_ID,BRANCH_ID,NOTATION)");

            strSql.Append(" values (");
            strSql.Append("@ID,@TITLE,@APPLICANT,@APPLICANT_NAME,@STATE,@END_TIME,@REAL_TIME,@REMARK,@APPLICANT_TIME,@APPROVER,@APPROVER_NAME,@ORDER_ID,@BRANCH_ID,@NOTATION)");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, model.ID);
            db.AddInParameter(dbCommand, "TITLE", DbType.String, model.TITLE);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "STATE", DbType.String, model.STATE);
            db.AddInParameter(dbCommand, "END_TIME", DbType.DateTime, model.END_TIME);
            db.AddInParameter(dbCommand, "REAL_TIME", DbType.DateTime, model.REAL_TIME);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "APPLICANT_TIME", DbType.DateTime, model.APPLICANT_TIME);
            db.AddInParameter(dbCommand, "APPROVER", DbType.Int32, model.APPROVER);
            db.AddInParameter(dbCommand, "APPROVER_NAME", DbType.String, model.APPROVER_NAME);
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.Int32, model.ORDER_ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "NOTATION", DbType.String, model.NOTATION);
            int result;
            object obj = db.ExecuteScalar(dbCommand);
            if (!int.TryParse(obj.ToString(), out result))
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Procurement.T_PUB_PURCHASE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_PUB_PURCHASE set ");
            strSql.Append("TITLE=@TITLE,");
            strSql.Append("APPLICANT=@APPLICANT,");
            strSql.Append("APPLICANT_NAME=@APPLICANT_NAME,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("END_TIME=@END_TIME,");
            strSql.Append("REAL_TIME=@REAL_TIME,");
            strSql.Append("REMARK=@REMARK,");
            strSql.Append("APPLICANT_TIME=@APPLICANT_TIME,");
            strSql.Append("APPROVER=@APPROVER,");
            strSql.Append("APPROVER_NAME=@APPROVER_NAME,");
            strSql.Append("ORDER_ID=@ORDER_ID,");
            strSql.Append("BRANCH_ID=@BRANCH_ID,");
            strSql.Append("NOTATION=@NOTATION");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, model.ID);
            db.AddInParameter(dbCommand, "TITLE", DbType.String, model.TITLE);
            db.AddInParameter(dbCommand, "APPLICANT", DbType.Int32, model.APPLICANT);
            db.AddInParameter(dbCommand, "APPLICANT_NAME", DbType.String, model.APPLICANT_NAME);
            db.AddInParameter(dbCommand, "STATE", DbType.String, model.STATE);
            db.AddInParameter(dbCommand, "END_TIME", DbType.DateTime, model.END_TIME);
            db.AddInParameter(dbCommand, "REAL_TIME", DbType.DateTime, model.REAL_TIME);
            db.AddInParameter(dbCommand, "REMARK", DbType.String, model.REMARK);
            db.AddInParameter(dbCommand, "APPLICANT_TIME", DbType.DateTime, model.APPLICANT_TIME);
            db.AddInParameter(dbCommand, "APPROVER", DbType.Int32, model.APPROVER);
            db.AddInParameter(dbCommand, "APPROVER_NAME", DbType.String, model.APPROVER_NAME);
            db.AddInParameter(dbCommand, "ORDER_ID", DbType.Int32, model.ORDER_ID);
            db.AddInParameter(dbCommand, "BRANCH_ID", DbType.Int32, model.BRANCH_ID);
            db.AddInParameter(dbCommand, "NOTATION", DbType.String, model.NOTATION);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_PUB_PURCHASE ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, ID);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Procurement.T_PUB_PURCHASE GetModel(string ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TITLE,APPLICANT,APPLICANT_NAME,STATE,END_TIME,REAL_TIME,REMARK,APPLICANT_TIME,APPROVER,APPROVER_NAME,ORDER_ID,BRANCH_ID,NOTATION from T_PUB_PURCHASE ");
            strSql.Append(" where ID=@ID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ID", DbType.String, ID);
            Model.Procurement.T_PUB_PURCHASE model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TITLE,APPLICANT,APPLICANT_NAME,STATE,END_TIME,REAL_TIME,REMARK,APPLICANT_TIME,APPROVER,APPROVER_NAME,ORDER_ID,BRANCH_ID,NOTATION ");
            strSql.Append(" FROM T_PUB_PURCHASE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<Model.Procurement.T_PUB_PURCHASE> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TITLE,APPLICANT,APPLICANT_NAME,STATE,END_TIME,REAL_TIME,REMARK,APPLICANT_TIME,APPROVER,APPROVER_NAME,ORDER_ID,BRANCH_ID,NOTATION ");
            strSql.Append(" FROM T_PUB_PURCHASE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Model.Procurement.T_PUB_PURCHASE> list = new List<Model.Procurement.T_PUB_PURCHASE>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public Model.Procurement.T_PUB_PURCHASE ReaderBind(IDataReader dataReader)
        {
            Model.Procurement.T_PUB_PURCHASE model = new Model.Procurement.T_PUB_PURCHASE();
            object ojb;
            model.ID = dataReader["ID"].ToString();
            model.TITLE = dataReader["TITLE"].ToString();
            ojb = dataReader["APPLICANT"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLICANT = (int)ojb;
            }
            model.APPLICANT_NAME = dataReader["APPLICANT_NAME"].ToString();
            model.STATE = dataReader["STATE"].ToString();
            ojb = dataReader["END_TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.END_TIME = (DateTime)ojb;
            }
            ojb = dataReader["REAL_TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.REAL_TIME = (DateTime)ojb;
            }
            model.REMARK = dataReader["REMARK"].ToString();
            ojb = dataReader["APPLICANT_TIME"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPLICANT_TIME = (DateTime)ojb;
            }
            ojb = dataReader["APPROVER"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.APPROVER = (int)ojb;
            }
            model.APPROVER_NAME = dataReader["APPROVER_NAME"].ToString();
            ojb = dataReader["ORDER_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ORDER_ID = (int)ojb;
            }
            ojb = dataReader["BRANCH_ID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BRANCH_ID = (int)ojb;
            }
            model.NOTATION = dataReader["NOTATION"].ToString();
            return model;
        }

        #endregion  Method
    }
}
